package net.hyperz.showtime;

import net.hyperz.showtime.fragments.FavoritesList;
import net.hyperz.showtime.fragments.LineupGrid;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Main extends Activity implements ActionBar.TabListener {
	private Fragment mFragment;
    
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.main);
        
        ActionBar actionBar = this.getActionBar();
        
        actionBar.setDisplayHomeAsUpEnabled(true);
        //and remove the title no title for home button
        actionBar.setDisplayShowTitleEnabled(true);
        //set that navigation mode is tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        actionBar.addTab(actionBar.newTab()
        		.setIcon(R.drawable.ic_menu_calendar)
        		.setText(this.getString(R.string.menu_listings))
        		.setTabListener(this), true);
        actionBar.addTab(actionBar.newTab()
        		.setIcon(R.drawable.ic_menu_star)
        		.setText(this.getString(R.string.menu_favorites))
        		.setTabListener(this));
        //TODO: Future Release
        //actionBar.addTab(actionBar.newTab()
        //		.setIcon(R.drawable.ic_menu_listings)
        //		.setText(this.getString(R.string.menu_upcoming))
        //		.setTabListener(this));
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    
  	@Override
  	public boolean onOptionsItemSelected(MenuItem item) {
  		//Intent myIntent = new Intent();
  		
  		try {
  			switch (item.getItemId()) {
  			default:
  				return super.onOptionsItemSelected(item);
  			}
  		} catch (Exception e) {
  			Log.e("Main", e.getMessage().toString());
  			for (StackTraceElement traceElement : e.getStackTrace()) {
  				Log.e("Main", "  at " + traceElement.toString());
  			}
  			return false;
  		}
  	}
    
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
	}
    
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		Bundle args = new Bundle();
		String className = "";
		
		switch (tab.getPosition()) {
	      	case 0:
	      		className = LineupGrid.class.getName();
	      		break;
	      	case 1:
	      		className = FavoritesList.class.getName();
	      		break;
	      	default:
	      		break;
		}
		
		mFragment = this.getFragmentManager().findFragmentByTag(className);
		if (mFragment == null) {
			mFragment = Fragment.instantiate(this, className, args);
			ft.replace(R.id.main_frame, mFragment, className);
		} 
	}
	
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		ft.detach(mFragment);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle inState) {
		super.onRestoreInstanceState(inState);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		
		super.onSaveInstanceState(outState);
	}
}
